﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows;
using System.IO;
using System.Threading;
using System.Windows.Input;
using static DW.Library.Win32API;
using static log4net.Appender.ColoredConsoleAppender;
using System.Text.Json;
using System.Windows.Media.Imaging;

namespace DW.Library
{
    public class AppInfo
    {
        public delegate void AppInfoOperationDelegate(AppInfo appInfo);
        public event AppInfoOperationDelegate AppInfoCollectEvent;
        public event AppInfoOperationDelegate AppInfoCloseEvent;
        public event AppInfoOperationDelegate AppInfoShowPopEvent;

        List<string> SafeDirectorList = new List<string>()
        {
           @"C:\Program Files",
            @"C:\Program Files (x86)",
            @"C:\Windows"
        };

        public AppInfo()
        {

        }
        public bool IsRemovedApp { get; set; }
        public bool IsSafeDir
        {
            get
            {
                foreach (var item in SafeDirectorList)
                {
                    bool flag = ExePath.StartsWith(item);
                    if (flag)
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        public struct HwndManaged
        {
            public HwndManaged()
            {
            }
            private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam)
            {
                switch (msg)
                {
                    case WM_GETMINMAXINFO:
                        var minMaxInfo = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));

                        minMaxInfo.ptMinTrackSize.Y = 200;
                        Marshal.StructureToPtr(minMaxInfo, lParam, true);
                        break;
                }

                return CallWindowProcW(_oldWndProc, hwnd, msg, wParam, lParam);
            }

            private delegate IntPtr WndProcDelegate(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);

            public void Move(HwndManaged hwndManaged)
            {
                Rect = hwndManaged.Rect;
                Hwnd = hwndManaged.Hwnd;
                hwndManaged.Rect = new RECT();
                hwndManaged.Hwnd = IntPtr.Zero;
            }
            public void Set(IntPtr Hwnd)
            {
                this.Hwnd = Hwnd;
                GetWindowRect(Hwnd, out Rect);
            }
            public RECT Rect = new RECT();
            public IntPtr Hwnd = IntPtr.Zero;
            /// <summary> 虚拟现实页面中第三方App的窗口句柄（只有当IsVirtualView为True时可用） </summary>
            public IntPtr ChildHwnd = IntPtr.Zero;
            private IntPtr _oldWndProc = IntPtr.Zero;

        }
        public HwndManaged Managed;


        public Visibility CloseVisbility
        {
            get
            {
                if (Managed.Hwnd != IntPtr.Zero)
                {
                    return Visibility.Visible;
                }
                else
                {
                    return Visibility.Hidden;
                }

            }
        }

        public Process Process { get; set; }

        public DateTime StartTime { get; set; }
        public string FileName { get { return Path.GetFileName(ExePath); } }
        public string? ExePath { get; set; }

        public string ProcessNameAndClassName
        {
            get
            {
                return $"{ProcessName}|{ClassName}";
            }
        }
        private string _className;
        public string? ClassName
        {
            get
            {
                return _className;
            }
            set
            {
                _className = value;
            }
        }
        public string? ProcessName { get; set; }
        private WindowInfo _windowInfo;
        public WindowInfo WindowInfo
        {
            get
            {
                return _windowInfo;
            }
            set
            {

                _windowInfo = value;
                ClassName = ProcessUtil.GetClassName(_windowInfo.Hwnd);
            }
        }
        public BitmapSource Icon { get; set; }
        public string? IconPath { get; set; }

        public bool IsVirtualView { get; set; }
        /// <summary>
        /// 虚拟窗口是否已经被移除
        /// Whether the virtual window has been removed
        /// </summary>
        public bool VirtualViewIsRemoved { get; set; }
        /// <summary>
        /// 虚拟窗口里面的子窗体的App信息
        /// The App information of the subforms in the virtual window
        /// </summary>
        public AppInfo ChildrenAppInfo { get; set; }
        /// <summary>
        /// 是否特权
        /// app is elevated
        /// </summary>
        public bool HasElevated { get; set; }

        /// <summary>
        /// 是否是Setting页面
        /// Is it a Settings page
        /// </summary>
        public bool IsSettingPage { get; set; }
        /// <summary>
        /// 绑定的界面
        /// Bound view
        /// </summary>
        public object View { get; set; }
        /// <summary>
        /// 是NewTable页面
        /// It's the NewTable page
        /// </summary>
        public bool IsNewTableCard { get; set; }



        public int Index { get; set; }

        public ulong OriginStyle { get; set; }
        public Rect OriginRect { get; set; }


        private void Btn_Collect_Click(object sender, RoutedEventArgs e)
        {
            AppInfoCollectEvent?.Invoke(this);
        }

        private void Btn_Close_Click(object sender, RoutedEventArgs e)
        {
            AppInfoCloseEvent?.Invoke(this);
        }


    }
}
